<?php 
class taobaoke_api 
{ 
	function taobaoke_api_main($bot_id,$api_data_josn) 
	{ 
		global $mysql_conn,$v_file;
		//获取飞天侠的数据库信息
				$sql = "SELECT * from `taobaoke_set` limit 0,1";
				$result = mysql_query($sql,$mysql_conn);
				@$row=mysql_fetch_array($result);
				$taobaoke_host = $row['taobaoke_host'];//启动语定义
				$taobaoke_name = $row['taobaoke_name'];//发送间隔时间
				$taobaoke_password = $row['taobaoke_password'];//周期起始条数
				$taobaoke_data = $row['taobaoke_data'];//周期循环条数
				$taobaoke_table = $row['taobaoke_table'];//图片等待时间
				$taobaoke_pid_a = $row['taobaoke_pid'];//主PID
				
		$api_data_data = json_decode($api_data_josn);//转化上报数据从josn格式转化为数组
		$post_type = $api_data_data->{'post_type'};//截取消息类型
		if ($post_type == "send_message") {
			$content = $api_data_data->{'content'};//获取发送的内容
			if ($content == $taobaoke_host) {
				
				//获取个人PID信息
				$sql = "SELECT * from `taobaoke_id` WHERE `taobaoke_bot` = '".$bot_id."'";
				$result = mysql_query($sql,$mysql_conn);
				@$row=mysql_fetch_array($result);
				$taobaoke_pid_c = $row['taobaoke_pid'];
				$taobaoke_pid_b = $row['taobaoke_form'];
				$taobaoke_pid = "mm_".$taobaoke_pid_a."_".$taobaoke_pid_b."_".$taobaoke_pid_c;
				//开始查询飞天侠数据库
				$sql = "SELECT * from `ftxia_items` limit ".$taobaoke_password.",".$taobaoke_data;
				
				$result = mysql_query($sql,$mysql_conn);
				while(@$row=mysql_fetch_array($result)) {
					$title = $row['title'];//标题
					$pic_url = $row['pic_url'];//图片地址
					$intro = $row['intro'];//简介
					$price = $row['price'];//售价
					$quan = $row['quan'];//代金券价格
					$Quan_condition = $row['Quan_condition'];//代金券简介
					$quanurl = $row['quanurl'];//代金券地址
					$click_url = $row['click_url'];//下单连接
					$price_new = $price-$quan;//优惠后价格
					//对代金券地址进行重新编译
					$refer_url = parse_url($quanurl); 
					$params = $refer_url['query']; 
					$arr = array(); 
					if(!empty($params)) {
						$paramsArr = explode('&',$params);
						foreach($paramsArr as $k=>$v) {
							$a = explode('=',$v);
							$arr[$a[0]] = $a[1];
						}
					}
					$arr['pid'] = $taobaoke_pid;
					$quanurl_new = "http://uland.taobao.com/coupon/edetail?activityId=".$arr['activityId']."&pid=".$arr['pid']."&itemId=".$arr['itemId']."&src=".$arr['src']."&dx=".$arr['dx'];
					$text = "";
					$text .= "【商品】".$title."\n";
					$text .= "----------------------------\n";
					$text .= "【价格】".$price." 包邮\n";
					$text .= "【优惠券】".$quan." 元\n";
					$text .= "【券后价格】".$price_new." 元\n";
					$text .= "【领券购买】".$quanurl_new."\n";
					$text .= "【推荐理由】".$intro."\n";
					//获取发送的URL
					$sql_server = "SELECT * from `bot` WHERE `bot_id` = '".$bot_id."' ";
					$result_server = mysql_query($sql_server,$mysql_conn);
					@$row_server = mysql_fetch_array($result_server);
					$bot_server_id = $row_server['bot_server_id'];
					$sql_url = "SELECT * from `server` WHERE `server_id` = '".$bot_server_id."' ";
					$result_url = mysql_query($sql_url,$mysql_conn);
					@$row_url = mysql_fetch_array($result_url);
					$server_url = $row_url['server_url'];
					//开始循环发送
					$sql_taobaoke = "SELECT * from `taobaoke` WHERE `taobaoke_bot` = '".$bot_id."' and `taobaoke_state` = '1'";
					$result_taobaoke = mysql_query($sql_taobaoke,$mysql_conn);
					while(@$row_taobaoke=mysql_fetch_array($result_taobaoke)) {
						$taobaoke_use = $row_taobaoke['taobaoke_use'];
						$taobaoke_use_arr = explode(',',$taobaoke_use);
						$taobaoke_use_count=count($taobaoke_use_arr); 
						for($i=0;$i<=$taobaoke_use_count;$i++) 
						{
							$taobaoke_use_name = $taobaoke_use_arr[$i];//获取每一个对象
							//这里是调用API进行发送
							$urlpic = $server_url."openwx/send_group_message?displayname=".urlencode($taobaoke_use_name)."&media_path=".urlencode($pic_url)."&client=".$bot_id;
							$urltxt = $server_url."openwx/send_group_message?displayname=".urlencode($taobaoke_use_name)."&content=".urlencode($text)."&client=".$bot_id;
							
							sleep($taobaoke_name);
							$html = file_get_contents($urlpic);
							sleep($taobaoke_table);
							$html = file_get_contents($urltxt);
						}
					}
					//结束循环发送		
					

				}
				//结束
			}			
		}
	}
}

